package timers

import (
	"time"
)

type MyTimer interface {
	Start()
	ReStart()
	Stop()
}

func MakeTimerInformers() (chan interface{}, chan interface{}){
	return make(chan interface{}), make(chan interface{})
}

type VCStartTimer struct {
	state    int // 0=>init; 1=>start; 2=>stop;
	duration time.Duration
	informer chan interface{}
	pause    chan interface{}
	ticker   *time.Ticker
}

func GetVCStartTImer(duration time.Duration, ch chan interface{}, pause chan interface{}) *VCStartTimer {
	return &VCStartTimer{
		state:    0,
		duration: duration,
		informer: ch,
		pause:    pause,
	}
}

func (timer *VCStartTimer) Start() {
	timer.state = 1
	timer.ticker = time.NewTicker(timer.duration)
	select {
	case <- timer.ticker.C:
		timer.informer <- struct{}{}
		close(timer.pause)
	}
}

func (timer *VCStartTimer) Restart() {
	timer.ticker.Stop()
	select {
	case <- timer.ticker.C:
		timer.informer <- struct{}{}
		close(timer.pause)
	}
}

func (timer *VCStartTimer) Stop() {
	timer.state = 2
	timer.pause <- NewTimerPause("VCStartTimer")
	timer.ticker.Stop()
	close(timer.informer)
}
